package com.njwb.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Description: 类的描述
 * Author: yuandeyin@itany.com
 * Date: 2025-08-07 14:11
 * Version: 1.0
 */
public class TestConstructor {
    public static void main(String[] args) {
        try {
            Class personClass = Class.forName("com.njwb.reflect.Person");
            //反射实例化对象
//            Person person = (Person) personClass.newInstance();
//            System.out.println(person);
            //反射调用Person(String name)构造方法，实例化对象
            //1.获取指定的构造器
            //只要获取带有1个String类型参数的构造器
            Constructor constructor = personClass.getDeclaredConstructor(String.class);
            //执行构造器
            Person person = (Person) constructor.newInstance("小白");
            System.out.println(person);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
